package system

import (
	"awesomeProxy/system/helper"
	"errors"
	"strconv"
	"strings"
)

const COOKIE_UNIQUE_ID = "AWESOME_PROXY_UNIQUE_ID"

type HtmlParser struct {
	Header  map[string]interface{}
	Content string
}

func (parser *HtmlParser) GetAllCookie() map[string]string {
	res := make(map[string]string)
	if cookie, ok := parser.Header["Cookie"]; ok {
		cookieList := strings.Split(cookie.(string), ";")
		for _, item := range cookieList {
			itemList := strings.Split(item, "=")
			if len(itemList) == 2 {
				res[strings.TrimSpace(itemList[0])] = strings.TrimSpace(itemList[1])
			}
		}
		return res
	} else {
		return map[string]string{}
	}
}
func (parser *HtmlParser) GetCookie(key string) string {
	if value, ok := parser.GetAllCookie()[key]; ok {
		return value
	}
	return ""
}

func (parser *HtmlParser) QueryParams() map[string]string {
	queryParamsStringList := strings.Split(parser.Header["REQUEST_URL"].(string), "?")
	if len(queryParamsStringList) == 1 {
		return map[string]string{}
	}
	queryParamsString := queryParamsStringList[1]
	res := make(map[string]string)
	queryParamsList := strings.Split(queryParamsString, "&")
	for _, value := range queryParamsList {
		paramsInfo := strings.Split(value, "=")
		if len(paramsInfo) == 2 {
			res[paramsInfo[0]] = paramsInfo[1]
		} else {
			res[paramsInfo[0]] = ""
		}
	}
	return res
}

func HttpUnPackage(body string) (HtmlParser, error) {
	parse := HtmlParser{
		Header: map[string]interface{}{},
	}
	list := strings.Split(body, "\r\n")
	if strings.Index(list[0], "HTTP/") == -1 {
		return parse, errors.New("解析失败")
	}
	firstLines := strings.Split(list[0], " ")
	if len(firstLines) != 3 {
		return parse, errors.New("解析失败")
	}
	parse.Header["REQUEST_METHOD"] = firstLines[0]
	parse.Header["REQUEST_URL"] = firstLines[1]
	parse.Header["REQUEST_PROTOCOL"] = firstLines[2]

	for index, line := range list {
		if index == 0 {
			continue
		} else {
			//空行后面的就为响应体了
			//if line=="" {
			//	if contentLength,ok:=parse.Header["Content-Length"];ok{
			//		contentLengthInt,err1 := strconv.Atoi(contentLength.(string))
			//		if err1!=nil {
			//			return parse,errors.New("解析失败")
			//		}
			//		parse.Content = body[len(body)-contentLengthInt:len(body)]
			//		break
			//	}else {
			//		return parse,errors.New("解析失败")
			//	}
			//
			//}
			headers := strings.Split(line, ":")
			if len(headers) != 2 {
				//return parse,errors.New("解析失败")
			} else {
				parse.Header[headers[0]] = headers[1][1:len(headers[1])]
			}
		}
	}
	return parse, nil
}
func WriteHttpCookieRes(uniqueId string, expiredSecond int) string {
	expiredAt := int(helper.NowInt()) + expiredSecond
	expiredAtString := helper.ParseGtmTime(int64(expiredAt))
	data := "HTTP/1.1 302 Found" +
		"\r\nLocation: /" +
		"\r\nAccept: application/json, text/plain, */*" +
		"\r\nSet-Cookie: {}={}; expires={}; Max-Age={}; path=/" +
		"\r\nContent-Length: 6" +
		"\r\n" +
		"\r\nhello~"
	return helper.Format(data, COOKIE_UNIQUE_ID, uniqueId, expiredAtString, strconv.Itoa(expiredSecond))
}
